<?php

namespace app\index\controller;

use app\BaseController;
use think\facade\View;

use function PHPSTORM_META\type;

class My extends BaseController
{

    public function index()
    {
        $state = "aaabbb";


        $query = http_build_query([
            'response_type' => 'code',
            'client_id' => 'myawesomeapp',
            'redirect_uri' => 'http://127.1.1.226/index/my/callback',
            'scope' => 'email',
            'state' => $state,
        ]);

        // return redirect('http://127.1.1.226/index/oauth/authorize/?' . $query);

        $url = 'http://127.1.1.226/index/oauth/authorize/?' . $query;

        View::assign('url', $url);

        return View::fetch();
    }



    //刷新访问令牌(access token)
    public function refresh()
    {

        $refresh_token = "def50200880fecccb76299ab8556c48f8df447fe02c578beb20d189390fdae676eb85204ed496bf08d0b1bde81e59903fb569879385b48ab57af0c63115199d492d9da430851ab3b629bad6ff29ac8a2600dd57580ea62b06a74f8eca99e0df9a4ad33cf1c0ef5c9d6ad5dc35dbd30a59a2527a4fafa6990e248e29cfe7b62db9a1da746b774ade70339e65dbd125bff4fd196309229efc0d38b78ee53cf09c4f2b994099e8a41f9db5021acaf14583fd9be182707f01a6768a2fe17dd770a1749befd342da0c32c4cd6c0d1266a30bbcc89f6fda80282d3803c749dd75b15509d1064a9de2d80200aa65af5fc822b1434e13fb35e0bfc378c78dc37f3894c4d05959113b389e3adc7518681f8a9b746f3d6d1fe24dbe2a2331484ee701e9faf6e4a993343cd1c8b3c2f2dbaed5f16f01d2f16aefef900e3638f63ade0378dbe9b9d1fe376bb411aa209e870684fd507bd390e973f86a099a4946974d550cd504e462a7072b562a7517769c40497304585d36f3437";



        $http = new \GuzzleHttp\Client;
    
        $response = $http->post('http://127.1.1.226/index/oauth/refresh/', [
            'form_params' => [
                'grant_type' => 'refresh_token', // 注意这里是 refresh_token
                'refresh_token' => $refresh_token,
                'client_id' => 'myawesomeapp',
                'client_secret' => 'abc123',
                'scope' => '',
            ],
        ]);
        
        dump(json_decode($response->getBody()));exit();

        return (string)$response->getBody();


    }



    // 客服端授权
    public function client()
    {
        $http = new \GuzzleHttp\Client;

        $response = $http->post('http://127.1.1.226/index/oauth/client', [
            'form_params' => [
                'grant_type' => 'client_credentials',
                'client_id' => 'myawesomeapp',
                'client_secret' => 'abc123',
                'scope' => '',
            ],
        ]);

        // return (string)$response->getBody();

        $res = json_decode($response->getBody(), true);


        $token_type = $res['token_type'];
        $access_token = $res['access_token'];

        dump($res);


        $this->send_t($access_token);

    }


    public function callback()
    {

        dump("这是回调页面");

        $code = $this->request->param('code');
        $state = $this->request->param('state');

        if(!$code)
        {

            $access_token='eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiJteWF3ZXNvbWVhcHAiLCJqdGkiOiI3Nzc3YTZmNmQ1N2M5N2Q4NjY0MDM2ZDQwOThkNmU4YjI0ZGJkNGRkMDU3MDUxNGY3ZjFkYjFlOGFkYTZjM2M2MzJlOGNiMmMwY2Q0MTViNCIsImlhdCI6MTcyMDM0NDQ1Ni4wNDgyMjYsIm5iZiI6MTcyMDM0NDQ1Ni4wNDgyMzIsImV4cCI6MTcyMDM0ODA1Ni4wMzkxNzIsInN1YiI6IjEiLCJzY29wZXMiOlsiZW1haWwiXX0.ZYz_VJ5wMjR8vVAmOEQaMJs-1evoQr8HbZfq6VD827crGi_7wcCxS7KM9AEgigQmrDp-gh9HvcNUk2V_AuQCJAjjxckdhOhcpCcctQMe3mIgGruGvP7rKOcxfhDBaiz0uA-nf5h5_xbHDnDkobctmjgCwV7KCxNz4cqpakDwK6Rbb4nADGoLITF7vcp5Fsr5t0p1naEraZlhVorydubcWB6lJ7buRlhDdNUSP8YDEbOThBh3LzFchGgsCwMFFEE0mbA9eXwIn6p-QQG_iPRiNpieE49Q-_aPDvB_zwSp83vKDg3LRxPhx65h1qINjx_fq8euJijKL7BUQbtCcucYfQ';
            dump("隐式模式");
            $this->send_t($access_token);
            exit();

        }
        

        //如果有已有token

            //用code换取token
            $http = new \GuzzleHttp\Client;
            $response = $http->post('http://127.1.1.226/index/oauth/issueToken/', [
                'form_params' => [
                    'grant_type' => 'authorization_code',
                    'client_id' => 'myawesomeapp',
                    'client_secret' => 'abc123',
                    'redirect_uri' => 'http://127.1.1.226/index/my/callback',
                    'code' => $this->request->get("code"), // 批准请求后服务端颁发的授权码
                ],
            ]);


            $res = json_decode($response->getBody(), true);


            $token_type = $res['token_type'];
            $access_token = $res['access_token'];

            dump($res);

       
  

        $this->send_t($access_token);





        // return (string)$response->getBody();
    }




    //授权码模式请求
    public function authorize()
    {

        $state = "aaabbb";


        $query = http_build_query([
            'response_type' => 'code',
            'client_id' => 'myawesomeapp',
            'redirect_uri' => 'http://127.1.1.226/index/my/callback',
            'scope' => '*',
            'state' => $state,
        ]);

        return redirect('http://127.1.1.226/index/oauth/authorize/?' . $query);


    }


    //用token直接请求
    public function user()
    {
        $http = new \GuzzleHttp\Client;
        $access_token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiJteWF3ZXNvbWVhcHAiLCJqdGkiOiJkNDc1YzNkM2I2ZmU4ODIwNjc5MmY0MTE0NmEzNWJjOWZjMjE3MjM3YzNjYzg5YWM4NzE4ODE3Mjc2ZTI0MTAwNTIwN2EwMDAzOTQ3ZTgyNSIsImlhdCI6MTcyMDI0MTU4My4yNjgwMTMsIm5iZiI6MTcyMDI0MTU4My4yNjgwMTYsImV4cCI6MTcyMDI0NTE4My4yNjUyMjQsInN1YiI6IjEiLCJzY29wZXMiOlsiZW1haWwiXX0.bu0vMilG_LzBQU3tQp8l8C7aNLeRpeI9YFRJaoPA3fuuFXoODcsNH4sIQetYp_tRoXlRzupw2ALd51mXzdFwFtC2vHb3Smd-xyOiYo8sq2AcNR0eBJJcvc12XKTuCzy2IwGRHZSS8dd-nebh9JGhsJYjRPKLd8LpI5fEL68M5JoMAfzkOhexLjbE0aHTqQN2JzioTm5LsJEeMyqs3jIfVHc2OOuUgRj6w9a1wQ3kkDTZv_-cTRxL0cFhbtct6pcZnNzXeeZu6sm74Qc6fnvJI6O_X-MYqbeA19gB33l8rvPVZZT-I8UCKsQ3vonxBRNgXqJYh6cYzn-dvQnSxy2yww';

        $response = $http->request('GET', 'http://127.1.1.226/index/api/user', [
            'headers' => [
                'Accept' => 'application/json',
                'Authorization' => 'Bearer ' . $access_token, // 注意 Bearer 后带一个空格
            ],
            'form_params' => [
                'scopes' => 'email',

            ],
        ]);
        dump((string)$response->getBody());
    }


    //隐式授权模式
    public function implicit()
    {
        $state = "implicit_cc";

        
        $query = http_build_query([
            'response_type' => 'token',
            'client_id' => 'myawesomeapp',
            'redirect_uri' => 'http://127.1.1.226/index/my/callback',
            'scope' => '',
            'state' => $state,
        ]);

        return redirect('http://127.1.1.226/index/oauth/implicit/?' . $query);
    }

    //密码模式获取token
    public function password()
    {

        // echo "123";exit();


        $http = new \GuzzleHttp\Client;

        $response = $http->post('http://127.1.1.226/index/oauth/password/', [
            'form_params' => [
                'grant_type' => 'password', // 注意这里是 password
                'client_id' => 'myawesomeapp',
                'client_secret' => 'abc123',
                'username' => 'alex', // 用户名
                'password' => 'whisky', // 密码
                'scope' => 'email', // ''?* 代表所有权限
            ]
        ]);


        $res = json_decode($response->getBody(), true);


        $token_type = $res['token_type'];
        $access_token = $res['access_token'];

        dump($res);

        $this->send_t($access_token);
    }


    function send_t($access_token)
    {
        $http = new \GuzzleHttp\Client;

        $response = $http->request('POST', 'http://127.1.1.226/index/api/user', [
            'headers' => [
                'Accept' => 'application/json',
                'Authorization' => 'Bearer ' . $access_token, // 注意 Bearer 后带一个空格
            ],
            'form_params' => ['scope' => 'email'],
        ]);

        dump((string)$response->getBody());
    }
}
